package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.Goods;
import com.yaowk.api.mall.clothing.model.GoodsImage;
import com.yaowk.api.mall.clothing.service.IGoodsService;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.file.entity.TSource;
import com.yaowk.service.mall.clothing.common.StatusDict;
import com.yaowk.service.mall.clothing.dao.GoodsDao;
import com.yaowk.service.mall.clothing.dao.GoodsImageDao;
import com.yaowk.service.mall.clothing.dao.GoodsVarietyDao;
import com.yaowk.service.mall.clothing.entity.CmGoods;
import com.yaowk.service.mall.clothing.entity.CmGoodsClass;
import com.yaowk.service.mall.clothing.entity.CmGoodsImage;
import com.yaowk.service.mall.clothing.entity.CmGoodsImageId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods, CmGoods, Integer> implements IGoodsService {

    @Autowired
    private GoodsDao dao;
    @Autowired
    private GoodsVarietyDao goodsVarietyDao;
    @Autowired
    private GoodsImageDao goodsImageDao;

    @Override
    public PageResp<Goods> findAll(Byte status, String keyword, PageReq pageReq) {
        Goods cmGoods = new Goods();
        if (isNotEmpty(status)) {
            cmGoods.setStatus(status);
        }
        if (isNotEmpty(keyword)) {
            keyword = "%" + keyword + "%";
            cmGoods.setName(keyword);
        }
        return findAll(cmGoods, pageReq);
    }

    @Override
    public void recovery(Integer id) {
        CmGoods cmGoods = dao.findOne(id);
        cmGoods.setStatus(StatusDict.GOODS_RECOVERY);
        dao.save(cmGoods);
    }

    @Override
    public void down(Integer id) {
        CmGoods cmGoods = dao.findOne(id);
        cmGoods.setStatus(StatusDict.GOODS_DOWN);
        dao.save(cmGoods);
    }

    @Override
    public void saveImage(GoodsImage goodsImage) {
        CmGoodsImageId cmGoodsImageId = new CmGoodsImageId();
        cmGoodsImageId.setGoodsId(goodsImage.getGoodsId());
        cmGoodsImageId.setImageId(goodsImage.getImageId());
        CmGoodsImage cmGoodsImage = goodsImageDao.findOne(cmGoodsImageId);
        if (isNotEmpty(cmGoodsImage)) {
            cmGoodsImage.setSort(goodsImage.getSort());
        } else {
            cmGoodsImage = new CmGoodsImage();
            cmGoodsImage.setId(cmGoodsImageId);
            cmGoodsImage.setSort(goodsImage.getSort());
        }
        goodsImageDao.save(cmGoodsImage);
    }

    @Override
    public void up(Integer id) {
        CmGoods cmGoods = dao.findOne(id);
        cmGoods.setStatus(StatusDict.NORMAL);
        dao.save(cmGoods);
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmGoods model2Entity(Goods model) {
        CmGoods cmGoods = null;
        if (isNotEmpty(model)) {
            cmGoods = new CmGoods();
            cmGoods.setStatus(model.getStatus());
            cmGoods.setName(model.getName());
            cmGoods.setAutoReceive(model.getAutoReceive());
            cmGoods.setCostPrice(model.getCostPrice());
            cmGoods.setDetail(model.getDetail());
            if (isNotEmpty(model.getGoodsClassId())) {
                CmGoodsClass cmGoodsClass = new CmGoodsClass();
                cmGoodsClass.setId(model.getGoodsClassId());
                cmGoods.setGoodsClass(cmGoodsClass);
            }
            if (isNotEmpty(model.getSmallImageId())) {
                TSource tSource = new TSource();
                tSource.setId(model.getSmallImageId());
                cmGoods.setSmallImage(tSource);
            }
            cmGoods.setKeyword(model.getKeyword());
            cmGoods.setOriginalPrice(model.getOriginalPrice());
            cmGoods.setPrice(model.getPrice());
            cmGoods.setShipPrice(model.getShipPrice());
            cmGoods.setSort(model.getSort());
            cmGoods.setId(model.getId());
        }
        return cmGoods;
    }

    @Override
    protected Goods entity2model(CmGoods model) {
        Goods goods = null;
        if (isNotEmpty(model)) {
            goods = new Goods();
            goods.setStatus(model.getStatus());
            goods.setName(model.getName());
            goods.setAutoReceive(model.getAutoReceive());
            goods.setCostPrice(model.getCostPrice());
            goods.setDetail(model.getDetail());
            if (isNotEmpty(model.getGoodsClass())) {
                goods.setGoodsClassId(model.getGoodsClass().getId());
            }
            goods.setKeyword(model.getKeyword());
            goods.setOriginalPrice(model.getOriginalPrice());
            goods.setPrice(model.getPrice());
            goods.setShipPrice(model.getShipPrice());
            goods.setSort(model.getSort());
            goods.setId(model.getId());
        }
        return goods;
    }
}
